package com.colorlight.cloud.gateway.api.message.interceptor.trans.impl;


import com.colorlight.cloud.gateway.api.exception.ReflexException;
import com.colorlight.cloud.gateway.api.message.interceptor.trans.TransFromProtoInterceptor;
import com.colorlight.cloud.gateway.api.message.utils.BaseType;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Demon
 * @date 25/12/2023  下午1:46
 */
@Slf4j
public class BaseTransFromProtoInterceptor implements TransFromProtoInterceptor {
	@Override
	public Boolean isInterceptor(Class<?> aClass) {
		return BaseType.isContains(aClass);
	}
	
	@Override
	public void addAttribute(Object transObject, Object value, Class<?> inputClass, String methodName) {
		Method method = null;
		try {
			method = transObject.getClass().getMethod(methodName,inputClass);
		} catch (NoSuchMethodException e) {
			log.error("", e);
		}
		if (method == null) {
			throw new ReflexException("获取方法失败");
		}
		try {
			method.invoke(transObject, value);
		} catch (IllegalAccessException | InvocationTargetException e) {
			throw new ReflexException("获取方法失败");
		}
	}
	
	
}
